package com.imagination.applet.domain.support.payment.service.impl;

import com.imagination.applet.domain.support.payment.service.PayService;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import java.util.Currency;


@Service
@RequiredArgsConstructor(onConstructor = @__({@Autowired}))
@Slf4j
public class WechatPayService implements PayService {

    @Value("${payment.wechat.app-id}")
    private String appId;
    @Value("${payment.wechat.merchant-id}")
    private String merchantId;
    /**
     * 商户证书序列号
     */
    @Value("${payment.wechat.merchant-serial-number}")
    public String merchantSerialNumber;
    /**
     * 商户API私钥路径
     */
    @Value("${payment.wechat.private-key-path}")
    public String privateKeyPath;
    /**
     * 商户APIV3密钥
     */
    @Value("${payment.wechat.api-v3-key}")
    public String apiV3Key;
    @Value("${payment.wechat.notify-url}")
    private String notifyUrl;

    private JsapiServiceExtension service;

    @PostConstruct
    public void setup() {
        // 加载平台证书（mchId：商户号,mchSerialNo：商户证书序列号,apiV3Key：V3密钥）
        RSAAutoCertificateConfig certificateConfig = new RSAAutoCertificateConfig.Builder()
                .merchantId(merchantId)
                .merchantSerialNumber(merchantSerialNumber)
                .apiV3Key(apiV3Key)
                .privateKeyFromPath(privateKeyPath)
                .build();
        service = new JsapiServiceExtension.Builder().config(certificateConfig).build();
    }

    @Override
    public PrepayWithRequestPaymentResponse prePay2(String openId, String traceNo, Integer amount, Currency currency, String description) {
        PrepayRequest request = getPrepayRequest(openId, traceNo, amount, currency, description);
        return service.prepayWithRequestPayment(request);
    }

    private PrepayRequest getPrepayRequest(String openId, String traceNo, Integer amount, Currency currency, String description) {
        PrepayRequest request = new PrepayRequest();
        Amount amt = new Amount();
        amt.setTotal(amount);
        amt.setCurrency(currency.getCurrencyCode());
        Payer payer = new Payer();
        payer.setOpenid(openId);
        request.setAmount(amt);
        request.setAppid(appId);
        request.setMchid(merchantId);
        request.setDescription(description);
        request.setNotifyUrl(notifyUrl);
        request.setOutTradeNo(traceNo);
        request.setPayer(payer);
        return request;
    }

    @Override
    public Transaction queryOrderByOutTradeNo(String traceNo) {
        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        request.setMchid(merchantId);
        request.setOutTradeNo(traceNo);
        Transaction transaction = null;
        return transaction;
    }
}
